// --------------------------------------------------------------------------------------------------------------------
// <copyright file="CommandMessage.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace Microsoft.Robotics.Hardware.DAC
{
    using System.IO;

    /// <summary>
    /// The Command message base class
    /// </summary>
    /// <typeparam name="T">Command type</typeparam>
    public abstract class CommandMessage<T> : ICommand
    {
        /// <summary>
        /// The command id
        /// </summary>
        private readonly byte commandType;

        /// <summary>
        /// The response code
        /// </summary>
        private readonly byte responseCode;

        /// <summary>
        /// Initializes a new instance of the <see cref="CommandMessage{T}"/> class.
        /// </summary>
        /// <param name="cmdId">The command id.</param>
        protected CommandMessage(byte cmdId)
            : this(cmdId, 0)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="CommandMessage{T}"/> class.
        /// </summary>
        /// <param name="cmdId">The command id.</param>
        /// <param name="responseCode">The response code.</param>
        protected CommandMessage(byte cmdId, byte responseCode)
        {
            this.commandType = cmdId;
            this.responseCode = responseCode;
        }

        /// <summary>
        /// Gets the command id.
        /// </summary>
        public byte CommandType
        {
            get
            {
                return this.commandType;
            }
        }

        /// <summary>
        /// Gets the response code.
        /// </summary>
        public byte ResponseCode
        {
            get
            {
                return this.responseCode;
            }
        }

        /// <summary>
        /// Gets a value indicating whether a response is required.
        /// </summary>
        /// <value>
        ///   <c>true</c> if a response is required; otherwise, <c>false</c>.
        /// </value>
        public abstract bool IsResponseRequired { get; }

        /// <summary>
        /// Prepares the request.
        /// </summary>
        /// <returns>
        /// <c>true</c> if the command can be scheduled, otherwise <c>false</c>.
        /// </returns>
        public virtual bool PrepareRequest()
        {
            return true;
        }

        /// <summary>
        /// Writes the request.
        /// </summary>
        /// <param name="writer">The writer.</param>
        public void WriteRequest(BinaryWriter writer)
        {
            // Write a place holder for the param length
            writer.Write((ushort)0);

            // Then write any parameters (including none)
            long startParamsPos = writer.BaseStream.Position;
            this.WriteParameters(writer);
            long endParamsPos = writer.BaseStream.Position;
            if (startParamsPos < endParamsPos)
            {
                ushort paramLength = (ushort)(endParamsPos - startParamsPos);

                // Then back up the stream and write the length in
                writer.BaseStream.Position = startParamsPos - sizeof(ushort);
                writer.Write(paramLength);
                writer.BaseStream.Position = endParamsPos;
            }
        }

        /// <summary>
        /// Closes the request.
        /// </summary>
        /// <param name="scheduled"><c>true</c> if the command was scheduled, otherwise <c>false</c>.</param>
        public virtual void CloseRequest(bool scheduled)
        {
        }

        /// <summary>
        /// Parses the parameters.
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <returns>An instance of the return type</returns>
        public abstract T ParseParameters(BinaryReader reader);

        /// <summary>
        /// Parses the response parameters.
        /// </summary>
        /// <param name="reader">The reader.</param>
        public abstract void ParseResponse(BinaryReader reader);

        /// <summary>
        /// Cancel a command.
        /// </summary>
        public abstract void Cancel();

        /// <summary>
        /// Writes the parameters
        /// </summary>
        /// <param name="writer">The writer.</param>
        protected abstract void WriteParameters(BinaryWriter writer);
    }
}
